package perso.perso.view.admin.school;

import java.io.IOException;
import java.util.List;

import org.apache.http.client.ClientProtocolException;

import com.sun.tools.internal.xjc.reader.xmlschema.bindinfo.BIConversion.User;

import perso.perso.MyAction;
import perso.perso.wsclient.HttpMiddlewareSchoolWSCall;
import perso.perso.wsclient.HttpMiddlewareUserWSCall;

public class AddSchoolAction extends MyAction{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private BeanSchool beanSchool;


	public BeanSchool getBeanSchool() {
		return beanSchool;
	}



	public void setBeanSchool(BeanSchool beanSchool) {
		this.beanSchool = beanSchool;
	}



	@Override
	public String execute() throws Exception {
		HttpMiddlewareSchoolWSCall h = new HttpMiddlewareSchoolWSCall();
		h.addSchool(beanSchool);

		return SUCCESS;
	}



	@Override
	public void validate() {

		if(beanSchool.getLibelleSchool() == null || beanSchool.getLibelleSchool().length() < 1 || "".equals(beanSchool.getLibelleSchool())){
			addFieldError( "beanSchool.libelle", "le libelle est obligatoire" );
		}
		if(beanSchool.getAdresse() == null || beanSchool.getAdresse().length() < 1 || "".equals(beanSchool.getAdresse().trim())){
			addFieldError( "beanSchool.adresse1", "l adresse est obligatoire" );
		}
		if(beanSchool.getCodePostal() == null || beanSchool.getCodePostal().toString().length() != 5){
			addFieldError( "beanSchool.codePostal", "le code postal est incorrect" );
		}
		
		if(beanSchool.getVille() == null || beanSchool.getVille().length() < 1 || "".equals(beanSchool.getVille())){
			addFieldError( "beanSchool.ville", "la ville est obligatoire" );
		}
		
		//recherche du nom
		HttpMiddlewareSchoolWSCall h = new HttpMiddlewareSchoolWSCall();
		try {
			
			List<BeanSchool> schools = h.getAllSchoolsByFilters(beanSchool.getLibelleSchool(), beanSchool.getAdresse1(), beanSchool.getAdresse2()
					, beanSchool.getAdresse3(), beanSchool.getCodePostal(), beanSchool.getVille());
			if(schools != null && schools.size() > 0){
				addActionError( "cette ecole existe deja" );
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			System.out.println(e.getMessage());
		}
	}



}
